﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace Gw2ToolNewSystem
{
    public partial class toolTip1 : Form
    {
        public List<Control> dynamicControls = new List<Control>();
        public toolTip1()
        {
            InitializeComponent();
        }

        private void toolTip1_Load(object sender, EventArgs e)
        {
            //load da ? load 1 kere oluyo abi
            this.Hide();

        }
        public void Clear()
        {
            this.Size = new Size(405, 115);
            foreach (var item in dynamicControls)
            {
                this.Controls.Remove(item);

            }
            pictureBox2.Image = Sifrele.resimListesi["Tango-recharge-darker.png"]; // tarzında :D
            pictureBox3.Image = Sifrele.resimListesi["Skill_point.png"];
            pictureBox4.Image = Sifrele.resimListesi["Initiative.png"];
            pictureBox2.Visible = true;
            pictureBox3.Visible = true;
            pictureBox4.Visible = true; 
            label3.Visible = true;
            label4.Visible = true;
            label5.Visible = true;

            dynamicControls.Clear();
        }
        public void SetPosition()
        {
            Point pos = Cursor.Position;

            this.DesktopLocation = new Point(pos.X - (this.Width / 2) , pos.Y + 10);
            //bunun olması mouse bi tooltip formuna bi picturebox a geldiği için sürekli enter leave oluyo :R buda bug :D düzeldi sanırım az önce öyle gördüm. Y ye +10 verdim mouse ile aynı yerde cıkmıyo form

            
            if (this.DesktopLocation.X + this.Width > Screen.PrimaryScreen.Bounds.Width)
            {
                this.DesktopLocation = new Point(Screen.PrimaryScreen.Bounds.Width - this.Width, this.DesktopLocation.Y);
            }

            if (this.DesktopLocation.Y + this.Height > Screen.PrimaryScreen.Bounds.Height)
            {
                this.DesktopLocation = new Point(this.DesktopLocation.X, Screen.PrimaryScreen.Bounds.Height - this.Height);
            }

            if (this.DesktopLocation.X < 0)
            {
                this.DesktopLocation = new Point(0, this.DesktopLocation.Y);
            }
        }
        public void ChangeContent(Content con)
        {


            label1.Text = con.Title;
            label2.Text = con.Text;
            label3.Text = con.CoolDown;
            label4.Text = con.SkillPoint;
            label5.Text = con.Initiative;

            if (con.CoolDown == "0") //eğer skillpoint yoksa ve cooldown varsa genede ikisini birden yapıcak bu if ikisini birden yok edicek :D evet şimdi farkettim
            {
                pictureBox2.Visible = false;
                label3.Visible = false;
            }
            else
            {
                pictureBox2.Image = Sifrele.resimListesi["Tango-recharge-darker.png"]; // tarzında :D
            }

            if (con.SkillPoint == "0")
            {
                pictureBox3.Visible = false;
                label4.Visible = false;
            }
            else
            {
                pictureBox3.Image = Sifrele.resimListesi["Skill_point.png"];
            }
            if (con.Initiative=="0")
            {
                pictureBox4.Visible = false;
                label5.Visible = false;
            }

            if (con.Picture != null)
            {
                pictureBox1.Visible = true;
                pictureBox1.Image = con.Picture;

                this.Height = con.Picture.Height + 15;
                if (this.Height < 45)
                    this.Height = 45;
            }
            else
            {
                pictureBox1.Visible = false;
                this.Height = 45;
            }

            if (con.Text != string.Empty && con.Text != null)
            {
                Size s = TextRenderer.MeasureText(con.Text, label2.Font);
                var splited = con.Text.Split('\n');
                Debug.WriteLine(14 * splited.Count());
                this.Width = pictureBox1.Width + 10 + 40 + s.Width;
                this.Height += 10 * splited.Count();
                label2.Size = new Size(s.Width, s.Height + (10 * splited.Length));
                int i = 1;
                int maxx = this.Width, maxy = this.Height;

                foreach (var v in con.MiniInfos)
                {
                    PictureBox pc = new PictureBox();
                    pc.Name = "pictureBoxInfo" + i;
                    pc.Location = new Point(77, 80 + (i * v.Key.Height));
                    pc.Size = v.Key.Size;
                    pc.Image = v.Key;
                    this.Height += pc.Image.Height + 10;



                    Label lb = new Label();
                    lb.Name = "labelinfo" + i;
                    lb.Text = v.Value;
                    s = TextRenderer.MeasureText(lb.Text, label2.Font);
                    lb.Size = s;
                    lb.Location = new Point(77 + 18, 80 + (i * 18));
                    lb.Font = label2.Font;
                    lb.ForeColor = label2.ForeColor;

                    this.Controls.Add(pc);
                    this.Controls.Add(lb);

                    dynamicControls.Add(pc);
                    dynamicControls.Add(lb);

                    if (maxx < pc.Location.X)
                        maxx = pc.Location.X;

                    if (maxy < pc.Location.Y)
                        maxy = pc.Location.Y;

                    if (maxx < lb.Location.X)
                        maxx = lb.Location.X;

                    if (maxy < lb.Location.Y)
                        maxy = lb.Location.Y;

                    i++;
                }
                this.Size = new Size(maxx + 10, maxy + 20);
            }

            SetPosition();
            this.Show();
        }


        private void toolTip1_MouseLeave(object sender, EventArgs e)
        {
            if (Elementalist.toolTip.CurrentControl != null)
            {
                Elementalist.toolTip.ClearControl(Elementalist.toolTip.CurrentControl);
            }
        }
    }
}
